<?php 
namespace Adminx\Controller;

class UserController extends AdminController{
	#列表
    public function index(){ 
        $role = M('Role')->getField('id,name');
        $obj = M('User');
        $list = $obj->order('id asc')->select();
        $this->assign('list', $list);   
        $this->assign('role', $role);    
    	$this->display();
    }

    #添加
    public function add(){
        if($_POST){
            $obj = D('User');
            if ($data = $obj->create()) {
                if ($list=$obj->add($data)) {

                    $obj = M('roleUser');
                    $tmp['role_id']=$data['group'];
                    $tmp['user_id']=$list;
                    $obj->add($tmp);

                    $url = U('User/index');
                    $this->success('管理员添加成功',$url);
                } else {
                    $this->error('管理员添加失败');
                }
            } else {
                $this->error($obj->getError());
            }
        }else{
            $obj = M('Role');
            $this->assign('group',$obj->select());
        	$this->display();
        }
    }

    #编辑
    public function edit(){
        if($_POST){
            $obj = D('User');
            if ($data = $obj->create()) {
                if($data['password']==''){
                    unset($data['password']);
                }else{
                    $data['password']=md5($data['password']);
                }
                if ($obj->save($data)) {
                    $role = M('roleUser');
                    $role->where('user_id='.$data['id'])->setField('role_id',$data['group']);

                    $url = U('User/index');
                    $this->success('管理员编辑成功',$url);
                } else {
                    $this->error('编辑失败');
                }
            } else {
                $this->error($obj->getError());
            }
        }else{
            $id = (int) I('get.id');
            if (!isset ($id)) {
                $this->error('参数错误');
            }
            $obj = M('User');
            $list = $obj->where('id=' . $id)->find();
            $this->assign('list', $list);
            $obj = M('Role');
            $this->assign('group',$obj->select());
            $this->display();
        }
    }

    //删除
    public function del() {
    	$id = $_POST['selectedids'];
        if(!isset($id)){
            $this->error('您没有选择任何信息！');
        }else{
            $where['id']=array('in',$id);
            $where['username']=array('neq','admin');
            $list = M('User')->where($where)->delete();
            if($list){
            	unset($map);
            	$map['uid']=array('in',$id);
        		M('UserLog')->where($map)->delete();

        		unset($map);
            	$map['user_id']=array('in',$id);
        		M('roleUser')->where($map)->delete();

                $url = U('User/index');
                $this->success('管理员编辑成功',$url);
            }else{
                $this->error('删除失败');
            }
        }        
    }

    //管理员登陆日志
    public function log(){
        $uid = $_GET['id'];
        if($uid!=''){
            $map['uid']=$uid;
        }
        $log = M('UserLog');        
        $count = $log->where($map)->count();
        import("Common.ORG.Page");
        $page = new \Page($count, 15);
        $show = $page->show();
        $list = $log->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        foreach ($list as $key => $value) {
            $list[$key]['username'] = M('User')->where('id='.$value['uid'])->getField('username');
        }
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->display();

    }

    //删除日志
    public function delog(){
        $id = $_POST['selectedids'];
        if(!isset($id)){
            $this->error('您没有选择任何信息！');
        }else{
            $where['id']=array('in',$id);
            $log=M('UserLog');
            $list = $log->where($where)->delete();
            if($list){
                $this->success('日志删除成功','reload');
            }else{
                $this->error('日志删除失败');
            }
        }        
    }

    //修改密码
    function password(){
        if(isset($_POST['dosubmit'])) {
            $oldpassword = I('post.oldpwd');
            $password = I('post.password');
            $condition['id']=$_SESSION['adminID'];
            $obj = M('User');
            $userpwd = $obj->where($condition)->field('password')->find();
            if($userpwd['password']!=md5($oldpassword)){
                $this->error('原始密码错误');
            }else{
                $data['password']=md5($password);
                $obj->where($condition)->save($data);
                $url = U('Index/index');
                $this->success('密码修改成功',$url);
            }
        }else{
            $this->display();
        }        
    }
}
?>